<template>
  <el-container class="layout-container cyber-grid">
    <!-- 顶部导航 -->
    <el-header height="60px">
      <div class="header-content">
        <div class="logo">
          <el-icon :size="30" style="color: var(--cyber-primary)"><Platform /></el-icon>
          <span class="logo-text neon-text">CMDB资产管理系统</span>
        </div>
        <div class="header-right">
          <el-dropdown @command="handleCommand">
            <span class="user-info">
              <el-icon><User /></el-icon>
              <span class="username">{{ username }}</span>
              <el-icon class="el-icon--right"><ArrowDown /></el-icon>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="profile">
                  <el-icon><User /></el-icon>
                  个人中心
                </el-dropdown-item>
                <el-dropdown-item command="settings">
                  <el-icon><Setting /></el-icon>
                  设置
                </el-dropdown-item>
                <el-dropdown-item divided command="logout">
                  <el-icon><SwitchButton /></el-icon>
                  退出登录
                </el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </div>
    </el-header>

    <el-container>
      <!-- 侧边导航 -->
      <el-aside width="220px">
        <el-menu
          :default-active="activeMenu"
          router
          class="el-menu-vertical"
        >
          <el-menu-item index="/dashboard">
            <el-icon><DataAnalysis /></el-icon>
            <span>仪表盘</span>
          </el-menu-item>
          
          <el-sub-menu index="cmdb">
            <template #title>
              <el-icon><Files /></el-icon>
              <span>CMDB资产管理</span>
            </template>
            <el-menu-item index="/host">
              <el-icon><Monitor /></el-icon>
              <span>主机管理</span>
            </el-menu-item>
            <el-menu-item index="/host-group">
              <el-icon><Connection /></el-icon>
              <span>主机组管理</span>
            </el-menu-item>
            <el-menu-item index="/project">
              <el-icon><Folder /></el-icon>
              <span>项目管理</span>
            </el-menu-item>
            <el-menu-item index="/environment">
              <el-icon><Setting /></el-icon>
              <span>环境管理</span>
            </el-menu-item>
            <el-menu-item index="/user">
              <el-icon><User /></el-icon>
              <span>用户管理</span>
            </el-menu-item>
            <el-menu-item index="/user-group">
              <el-icon><UserFilled /></el-icon>
              <span>用户组管理</span>
            </el-menu-item>
          </el-sub-menu>
        </el-menu>
      </el-aside>

      <!-- 主内容区 -->
      <el-main>
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'

const route = useRoute()
const router = useRouter()
const activeMenu = computed(() => route.path)
const username = ref(localStorage.getItem('username') || 'Admin')

const handleCommand = async (command) => {
  if (command === 'logout') {
    try {
      await ElMessageBox.confirm('确定要退出登录吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      
      // 清除登录状态
      localStorage.removeItem('isLoggedIn')
      localStorage.removeItem('username')
      localStorage.removeItem('remember')
      
      ElMessage.success('已退出登录')
      
      // 跳转到登录页
      router.push('/login')
    } catch {
      // 取消退出
    }
  } else if (command === 'profile') {
    ElMessage.info('个人中心功能开发中...')
  } else if (command === 'settings') {
    ElMessage.info('设置功能开发中...')
  }
}
</script>

<style scoped>
.layout-container {
  min-height: 100vh;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 100%;
  padding: 0 20px;
}

.logo {
  display: flex;
  align-items: center;
  gap: 15px;
}

.logo-text {
  font-size: 24px;
  font-weight: bold;
  color: var(--cyber-primary);
  font-family: 'Courier New', monospace;
  letter-spacing: 2px;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 20px;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  padding: 8px 16px;
  border-radius: 20px;
  background: rgba(0, 255, 159, 0.1);
  border: 1px solid rgba(0, 255, 159, 0.3);
  transition: all 0.3s;
}

.user-info:hover {
  background: rgba(0, 255, 159, 0.2);
  border-color: var(--cyber-primary);
  box-shadow: 0 0 10px rgba(0, 255, 159, 0.5);
}

.username {
  color: var(--cyber-primary);
  font-size: 14px;
  font-weight: bold;
}

.el-aside {
  background: var(--cyber-bg-secondary);
  overflow-x: hidden;
}

.el-menu-vertical {
  border-right: none;
  min-height: calc(100vh - 60px);
}

.el-main {
  min-height: calc(100vh - 60px);
}
</style>
